﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;





//怪物状态
public enum MonsterState { Normal, Toxicity }



//所有怪物基类
public class Monster : MonoBehaviour
{
    public MonsterState State { get; protected set; }
    public int hp;
    public float attack;
    public float defense;
    public float jumpHigh;
    public float stepOffset;
    public float normalAttackCD;

    //表示是否在地面
    public bool IsGround { get; set; }

    //表示是否碰到天花板
    public bool IsCeiling { get; set; }

    public float walkSpeed;
    public float runSpeed;
    public int jumpCountLimit = 1;

    [HideInInspector] public int jumpCount;

    //连续受伤(被连击)次数
    [HideInInspector] public int seriesTakeDamageCount = 0;

    [HideInInspector] public Rigidbody2D rigidBody2D;
    [HideInInspector] public MonsterAnimController animController;
    [HideInInspector] public MonsterBehaviorMode behaviorMode;

    protected float normalAttackCDTimer;
    private SpriteRenderer spriteRenderer;


    protected virtual void Awake()
    {
        rigidBody2D = GetComponent<Rigidbody2D>();
        animController = GetComponent<MonsterAnimController>();
        behaviorMode = GetComponent<MonsterBehaviorMode>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        normalAttackCDTimer = normalAttackCD;
        jumpCount = jumpCountLimit;
    }



    protected virtual void Start()
    {
        MonsterTaskProcessor.Instance.Register(this);
        InvokeRepeating("Timing", 0.0f, Time.fixedDeltaTime);
    }
  

    /// <summary>
    /// 水平方向移动
    /// </summary>
    public virtual void HorizontalMoveWalk(Vector3 horizontal)
    {
        if(horizontal != Vector3.left && horizontal != Vector3.right)
        {
            Debug.LogError("horizontal: " + horizontal);
            Debug.LogError("horizontal 只能是 Vector3.left 或Vector3.right!!");
            Debug.Break();
        }

        //转身
        if (transform.eulerAngles.y == 0 && horizontal == Vector3.left)
        {
            transform.eulerAngles = new Vector3(0.0f, 180.0f, 0.0f);
        }
        else if(transform.eulerAngles.y == 180 && horizontal == Vector3.right)
        {
            transform.eulerAngles = new Vector3(0.0f, 0.0f, 0.0f);
        }

        transform.Translate(horizontal * stepOffset * walkSpeed * Time.deltaTime, Space.World);
    }



    public virtual void HorizontalMoveRun(Vector3 horizontal)
    {
        if (horizontal != Vector3.left && horizontal != Vector3.right)
        {
            Debug.LogError("horizontal: " + horizontal);
            Debug.LogError("horizontal 只能是 Vector3.left 或Vector3.right!!");
            Debug.Break();
        }

        //转身
        if (transform.eulerAngles.y == 0 && horizontal == Vector3.left)
        {
            transform.eulerAngles = new Vector3(0.0f, 180.0f, 0.0f);
        }
        else if (transform.eulerAngles.y == 180 && horizontal == Vector3.right)
        {
            transform.eulerAngles = new Vector3(0.0f, 0.0f, 0.0f);
        }

        transform.Translate(horizontal * stepOffset * runSpeed * Time.deltaTime, Space.World);
    }



   /// <summary>
   /// 被打击后的处理
   /// </summary>
    public virtual void Hit(DamageInfo damageInfo, GameObject attacker)
    {
        hp -= damageInfo.damage;
        if (hp > 0)
        {
          
        }
        else
        {
            gameObject.SetActive(false);
            Debug.Log("GG");
        }
    }


    public void StartAttackCDTiming()
    {
        normalAttackCDTimer = 0.0f;
    }

    
    public bool IsInNormalAttackCD()
    {
        return normalAttackCDTimer < normalAttackCD;
    }


    protected virtual void Timing()
    {
        if (normalAttackCDTimer < normalAttackCD)
        {
            normalAttackCDTimer += Time.deltaTime;
        }
    }
}
